<?php
$objXajax->registerFunction("frmreportdetail_load");
$objXajax->registerFunction("frmreportdetail_productDetail");

function frmreportdetail_load($reporttype=1,$dateFrom='',$dateTo=''){     

   global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $addPath = '_'.$reporttype;
    $dateFrom = $dateFrom==''?SYSTEM_DATE  : $dateFrom;
    $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
    $datasum = array();
    $arrData = frmreportdetail_createArray($datasum,$reporttype,$dateFrom,$dateTo);
    
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_reportdetail_grid'.$addPath.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('frmreportdetail_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmreportdetail_tbl');");  
  //  $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmreportdetail_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                      
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax      
}

function frmreportdetail_createArray(&$datasum,$reporttype,$dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix;
    $orderby = ' order by h.timeend,r.name,u2.username ';
    if($reporttype==2){
       $orderby = ' order by r.name,h.timeend,u2.username '; 
    }elseif($reporttype==3){
       $orderby = ' order by u2.username,r.name,h.timeend ';  
    }
    $sql = " select r.name as roomname,h.timebegin as timebegin,h.timeend as timeend,
    DATE_FORMAT(h.timeend,'%Y-%m-%d') as dateget,DATE_FORMAT(h.timeend,'%h:%i') as timeget,UNIX_TIMESTAMP(h.timeend) 
    - UNIX_TIMESTAMP(timebegin) as duration,r.id as room_id, h.generate_id,
    u1.username startperson, u2.username as endperson,price,totalprice,h.flg_move as flg_move   
     from tbl_history_book_room$tbl_postfix as h join tbl_room$tbl_postfix r ON h.room_id = r.id join tbl_user as u1 
     ON u1.id = h.user_created join tbl_user as u2 ON u2.id = h.user_update 
     where datediff('$dateFrom',h.timeend)<=0 and datediff('$dateTo',h.timeend)>=0 $orderby ";//
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $totalsum = 0;
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $arr[$i]['productprice']= $arr[$i]['totalprice'] - $arr[$i]['price'] ;
          $arr[$i]['duration']= ConvertToHMS($arr[$i]['duration']);          
          $totalsum  += (int)$arr[$i]['totalprice'] ;
          $pricesum  += (int)$arr[$i]['price'] ;
     } 
         
    $datasum[0]['totalprice']= $totalsum;
    $datasum[0]['price']= $pricesum;
    $datasum[0]['productprice']= $totalsum-$pricesum;
    return  $arr  ;
} 

function frmreportdetail_productDetail($generate_id){
    global $objDbUpdate,$lang,$tbl_postfix,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    
    $sql = " select name as productname,num as num,price as price from tbl_history_product$tbl_postfix where generate_id = '$generate_id'  ";//
   // echo $sql; die();
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $pricesum  += (int)$arr[$i]['price'] ;
     }
     $datasum[0]['price']= $pricesum;
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_reportdetail_productdetail_grid.html');
    $oTbs->MergeBlock('blk_data', $arr); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('frmreportdetail_productDetail', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("$('#frmreportdetail_productDetail').dialog('open');");
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax             
    
} 

# end region process data
?>
